import { groupBy, shuffle } from "lodash-es";

export function sleep(time?: number) {
    return new Promise((r) => {
        setTimeout(() => {
            r(null)
        }, time||0);
    })
}

export function isUnDef(value:unknown):value is (undefined | null){
 return  value === undefined || value === null
}

export function cardDistanceField(data:{x:number,y:number},AB:'A'|'B',field:number,inPoint:boolean){
   let x =  0
   let y = 0
   if(AB === 'A'){
    y = window.innerHeight * (inPoint? 35:26.5) / 100
   } else {
    y = window.innerHeight * (inPoint? 43:51.5) / 100
   }
   if(field === 0){
    x = window.innerWidth * 8.3 / 100
   } 
   if(field === 1){
    x = window.innerWidth * 36.8 / 100
   } 
   if(field === 2){
    x = window.innerWidth * 65.3 / 100
   } 

 return {x:x - data.x,y:y - data.y}
}

export function findAndRemove<T>(arr:T[],item:T){
const index = arr.indexOf(item)
if(index >-1){
    arr.splice(index,1)
}
}

export function  choose(arr, count) {
    // 先进行分组
    const { first, other } = groupBy(arr, "type");
    return [
        // 从 first 组里选 1 个
        first[~~(Math.random() * first.length)],
        // 再从 other 组里随机选 count - 1 个（先洗牌，再按顺序取）
        ...shuffle(other).slice(0, count - 1)
    ];
}

